<?php
/**[前后台公用控制器]
 * @Author: chen
 * @Date:   2014-08-17 18:00:43
 * @Last Modified by:   chenryoku
 * @Last Modified time: 2014-11-16 22:35:30
 */
class ExtendAction extends Action{

  /**
   * [verify 验证码]
   * @return [type] [description]
   */
	public function verify()
	{
		  import('Class.Code',APP_PATH);
    	$code = new Code();
    	$code->show();
	}

	/**
	 * [down 下载]
	 * @return [type] [description]
	 */
	public function down()
	{
		$field = I('get.field','file');
		$aid = I('get.aid');
		$file = D('ArticleView')->get_field($aid,$field);
		header("Content-type:application/octet-stream");//二进制文件
		$fileName = basename($file);//获得文件名
		header("Content-Disposition:attachment;filename={$fileName}");//下载窗口中显示的文件名
		header("Accept-ranges:bytes");//文件尺寸单位
		header("Accept-length:".filesize($file));//文件大小
		readfile($file);//读出文件内容
	}

  /**
   * [keditor_upload 编辑器图片上传]
   * @return [type] [description]
   */
	public function keditor_upload()
	{
		// 上传类
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 314572800 ;// 设置附件上传大小
		$ext = explode('|', C('cfg_file'));
        $upload->allowExts  = $ext;// 设置附件上传类型
        $dir = I('get.dir');
        $savePath = './Data/Uploads/'.$dir.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
        $upload->savePath = $savePath;
        // 创建目录
        //is_dir($savePath) || mkdir($savePath,0777,true);
        // 执行上传
       	if(!$info=$upload->uploadOne($_FILES['imgFile'],$savePath))
       	{
       		// 上传错误提示错误信息
       		echo json_encode(array('error' => 1, 'message' =>"1".$upload->getErrorMsg()));
       	}
       	else
       	{
       		// 上传成功 获取上传文件信息
       		
       		$fileUrl = $info[0]['savepath'].$info[0]['savename'];
       		$keditor = pathinfo($fileUrl);
       		// 保存数据到数据库
       		$data=array(
       			'name'=>$keditor['filename'],
       			'ext'=>$keditor['extension'],
       			'path'=>$keditor['dirname'],
       			'size'=>filesize($fileUrl),
       			'addtime'=>time(),
       			'user_uid'=>session('uid'),
       		);
       		$id = D('Upload')->add_upload($data);
       		$_SESSION['keditor'][]=$id;
       		$fullPath = __ROOT__ . '/' . $fileUrl;
       		echo json_encode(array('error' => 0, 'url' => $fullPath));
       	}
		exit;
	}

  /**
   * [export_csv 导出csv]
   * @param  [type] $filename [description]
   * @param  [type] $data     [description]
   * @return [type]           [description]
   */
  public function export_csv($filename,$data) 
  { 
    header("Content-type:text/csv"); 
    header("Content-Disposition:attachment;filename=".$filename); 
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
    header('Expires:0'); 
    header('Pragma:public'); 
    echo $data; 
  } 
}